package com.pms.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.pms.entity.File;
import com.pms.util.DBHelp;
import com.pms.util.RowMapper;



public class SingleFileDao {
	
	DBHelp<File> db=new DBHelp<File>();
	public void saveSingleFile(File f) {
		
		String sql="INSERT INTO t_file(fileid,name,filename,size,type,createtime,t_folder_id,t_user_useid) VALUES(?,?,?,?,?,?,?,?)";
		db.executeSQL(sql,f.getFileid(),f.getName(),f.getFilename(),f.getSize(),f.getType(),f.getCreatetime(),f.getFileid(),f.getUserid());
		
	}
	public List<File> findAllByfId(String id) {
		String sql="SELECT t_file.id,`name`,filename,size,`type`,createtime,`t_folder_id`,username FROM t_file LEFT JOIN `t_user` ON `t_user`.id=`t_file`.`t_user_useid` WHERE `t_folder_id`=?";
		return db.executeQueryToList(sql, new RowMapper<File>(){

			public File mapRow(ResultSet rs) throws SQLException {
				File ft=new File();
				ft.setFileid(rs.getString("id"));
				ft.setFilename(rs.getString("filename"));
				//ft.setSize(rs.getLong("f_size"));
				ft.setName(rs.getString("name"));
				//ft.setMember_id(rs.getString("member_id"));
				ft.setFileid(rs.getString("filetype_id"));
				ft.setCreatetime(rs.getString("f_createtime"));
				ft.setUsername(rs.getString("username"));
				return ft;
			
			}
			
			
		}, id);
	}
	
	private class FileRowMapper implements RowMapper<File> {

		public File mapRow(ResultSet rs) throws SQLException {
			File ft=new File();
			ft.setFileid(rs.getString("id"));
			ft.setFilename(rs.getString("name"));
		//	ft.setSize(rs.getLong("size"));
			ft.setName(rs.getString("filename"));
		//	ft.setMember_id(rs.getString("user_id"));
			ft.setFileid(rs.getString("filetype_id"));
			ft.setCreatetime(rs.getString("createtime"));
			return ft;
		}
	
	}

	public void delFile(String id) {
		
		String sql="delete from t_file where id=?";
		db.executeSQL(sql,id);
	}
	
}
